לבנות תכנית שעונה על השאלה הבאה:
בהינתן 6 אבני דומינו, האם קיימת פירמידה של 3-2-1 כך שכל חצי של אבן דומינו בעל אותו מספר של חצי אבן הדומינו שנמצא מעליו.
I have seen this question at codility exam at tandemG company, Lod, Israel.
It was coding challenge.
I think the difficulty is medium.
I think it's a good array and understanding question.
שאלות מתוך הראיון
The winter summer problem:
Given an array of T of Temperatures(specified as integer numbers), partition the array into winter and summer that meets the condition that all temperatures in winter subarray is less than all the temperatures at summer subarray and makes the winter subarray shortest as possible.
You should return the length of winter subarray. Both winter and summer have to be at least one value. You can assume that there at least one partition that satisfies the condition.
Examples:
Given T = [ 5, -2, 3, 8, 6 ] the function should return 3 as after partitioning the array into winter: [5, -2, 3] and summer: [8, 6]. Because all temperatures in winter < all temperatures in summer.
Given T = [ -5, -5, -5, -42, 6, 12 ] the function should return 4 as after partitioning the array into winter: [-5, -5, -5, -42] and summer: [6, 12].
תשובות
הוסף תשובה
|
לצפיה בתשובות
יולי 2018
The idea is:
When we iterate the array from right to left,
we fill another array, lets call it minRight, with all the minimum temperatures at right sub-array at index i, the we iterate through original array from left to right, and save the max value of left part of array and compare it to min value of left array, we stop when max(left) > min(right).
1. שני חוטים שרצים על פונקציה שמעלה משתנה גלובלי באחד ומדפיסה אותו. מה התוצאות האפשריות.
. פונקציה שמקבלת מערך, את הגודל שלו ומספר. צריך למצוא אם המספר קיים במערך. אבל מכיוון שהתוכנית רצה על מעבד שפעולות השוואה בו הן מאוד יקרות, צריך לחסוך כמה שניתן בפעולות השוואה. (גם של הלולאה וגם של בדיקת המספר מול המערך)
תשובות
הוסף תשובה
|
לצפיה בתשובות
מרץ 2019
ניתן להריץ לולאת for ללא תנאי עצירה (וממילא לחסוך n השוואת). את הלולאה ניתן בכל זאת לעצור באופן הבא: שמור את הערך האחרון של המערך במשתנה זמני ובדוק האם הוא הערך הרצוי, במידה וכן, return, במידה ולא, הצב בסוף המערך את הערך שאותו אנו מחפשים ובצע השוואה אחת על כל איטרציה של הלולאה. במידה ונמצא הערך, return. ההחלפה של הערך האחרון תבטיח שהלולאה תיעצר בוודאות.